package com.hanter.android.radlib.util;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 类名：FileUtils <br/>
 * 描述：文件工具类
 * 创建时间：2021/08/20 15:38
 *
 * @author hanter
 * @version 1.0
 */
public class FileUtils {

    public static final String TAG = "FileUtils";

    @SuppressLint("SimpleDateFormat")
    private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMdd");

    public static final int DAY_TIME = 24 * 60 * 60 * 1000;

    public static boolean existsExternalStorage() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                && !Environment.isExternalStorageRemovable();
    }

    private static String genFileDate() {
        Date curr = new Date();
        long time = curr.getTime() % DAY_TIME / 100;
        String date = sDateFormat.format(curr);
        return String.format(Locale.US, "%s_%06d", date, time);
    }

    public static File createFile(File dir, String prefix, String suffix) throws IOException {
        File file = new File(dir, genFileName(prefix, suffix));
        if (!file.exists()) {
            File parent = file.getParentFile();
            if (parent == null || parent.exists() || parent.mkdirs()) {
                if (file.createNewFile()) {
                    Log.d(TAG, "create temp file.");
                } else {
                    Log.d(TAG, "temp file is existed.");
                }
            }
        }
        return file;
    }

    public static File createFile(String parent, String prefix, String suffix) throws IOException {
        return createFile(new File(parent), prefix, suffix);
    }

    private static String genFileName(String prefix, String suffix) {
        String dateName = genFileDate();
        StringBuilder sb = new StringBuilder();
        if (prefix != null) {
            sb.append(prefix);
            sb.append("_");
        }
        sb.append(dateName);
        if (suffix != null) {
            sb.append(suffix);
        }
        return sb.toString();
    }

    private static void copyStream(InputStream is, OutputStream os) throws IOException {
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) > 0) {
            os.write(buf, 0, len);
        }
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    public static void copy(File src, File dst) throws IOException {
        try (InputStream in = new FileInputStream(src)) {
            try (OutputStream out = new FileOutputStream(dst)) {
                copyStream(in, out);
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    public static void copy(FileDescriptor src, FileDescriptor dst) throws IOException {
        try (InputStream in = new FileInputStream(src)) {
            try (OutputStream out = new FileOutputStream(dst)) {
                copyStream(in, out);
            }
        }
    }

}
